package com.hc.framework.web.config;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.hc.framework.log.LogUtils;
import com.hc.framework.view.PhotoViewActivity;
import com.hc.framework.web.WebViewActivity;

import java.util.ArrayList;

/**
 * Created by Admin on 2017/3/1.
 * js通信接口
 */

public class ImageClickInterface {
    private Context context;

    public ImageClickInterface(Context context) {
        this.context = context;
    }


    /**
     * 点击网页里面的图片
     * @param imgUrl 所有图片的地址用#_#分割,确保误分割
     * @param cur 当前页面的url
     */
    @JavascriptInterface
    public void imageClick(String imgUrl, String cur) {
        //LogUtils.e("imgUrl",imgUrl);
        ArrayList<String> images = new ArrayList<>(); //图片参数
        String[] imgs = imgUrl.split("#_#");
        int index = 0;
        for(int i = 0 ; i < imgs.length ; i++){
            images.add(imgs[i]);
            if(cur.equals(imgs[i])){
                index = i;
            }
        }

        //1.新建一个intent,点击图片放大
        Intent intent = new Intent(context, PhotoViewActivity.class);

        //2.传递参数
        //2.1当前位置
        intent.putExtra(PhotoViewActivity.CURRENT_POINT_KEY,index);
        //2.2总的图片路径
        intent.putStringArrayListExtra(PhotoViewActivity.IMAGE_URL_LIST_KEY,images);
        //3.1跳转过去
        context.startActivity(intent);
    }

    @JavascriptInterface
    public void linkClick(String url,String needTitle) {
        if (!TextUtils.isEmpty(url)) {
            if(needTitle.equals("0")){
                WebViewActivity.loadUrl(context, url, false,false);
            }else{
                WebViewActivity.loadUrl(context, url, false,true);
            }
        }
    }
}